﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using Newtonsoft.Json;
using ProjectN.Core.Element.Def;
using ProjectN.Core.Instance.Data;

namespace ProjectN.Core.Element
{
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Rectangle : AdvancedElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("矩形位置"), JsonProperty]
        public required RRectangleF Rect { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo? Fill { get; set; }

        public Rectangle()
        {
            this.Load += Rectangle_Load;
        }

        private void Rectangle_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
            if(Fill != null)
                Fill.Data = data;
        }

        public override IEnumerable<IElement> Subelements
        => [
            new Polygon {
                Pen = Pen,
                Points = [
                    new System.Drawing.PointF(Rect.X, Rect.Y), new System.Drawing.PointF(Rect.X + Rect.W, Rect.Y),
                    new System.Drawing.PointF(Rect.X + Rect.W, Rect.Y + Rect.H), new System.Drawing.PointF(Rect.X, Rect.Y + Rect.H)
                ],
                Fill = Fill,
            }
        ];

        public override string ToString() => "矩形";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class Square : AdvancedElement
    {
        [Category("基本"), DisplayName("画笔"), JsonProperty]
        public required PenInfo Pen { get; set; }

        [Category("基本"), DisplayName("位置"), JsonProperty]
        public required PointF Position { get; set; }

        [Category("基本"), DisplayName("边长"), JsonProperty]
        public required int Size { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo? Fill { get; set; }
        public Square()
        {
            this.Load += Square_Load;
        }

        private void Square_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Pen.Data = data;
            if (Fill != null)
                Fill.Data = data;
        }

        public override IEnumerable<IElement> Subelements
        => [
            new Rectangle() { Pen = Pen, Rect = new RectangleF(Position, new System.Drawing.SizeF(Size, Size)), Fill = Fill }
        ];

        public override string ToString() => "正方形";
    }

    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class RoundedRectangle : AdvancedElement
    {
        [Category("基本"), DisplayName("边框"), JsonProperty]
        public required PenInfo Border { get; set; }

        [Category("基本"), DisplayName("填充"), JsonProperty]
        public required BrushInfo? Fill { get; set; } = null;

        [Category("基本"), DisplayName("矩形"), JsonProperty]
        public required RRectangleF Rectangle { get; set; }

        [Category("基本"), DisplayName("圆角半径"), JsonProperty]
        public required float RadiusX { get; set; }

        [Browsable(false), JsonProperty]
        public required float? RadiusY { get; set; } = null;
        public RoundedRectangle()
        {
            this.Load += RoundedRectangle_Load;
        }

        private void RoundedRectangle_Load(object sender, LoadEventArgs e)
        {
            Data = e.InstanceData;
        }

        protected override void UpdateData(InstanceData data)
        {
            Border.Data = data;
            if (Fill != null)
                Fill.Data = data;
        }

        public override IEnumerable<IElement> Subelements
        {
            get
            {
                GraphicsPath path = new();
                path.StartFigure();
                float radiusX = RadiusX;
                float radiusY = RadiusY ?? RadiusX;

                //x 坐标
                float[] itXs = [
                    Rectangle.X + radiusX,
                    Rectangle.X + Rectangle.W - 1 - radiusX,
                    Rectangle.X + Rectangle.W - 1
                ];
                //y 坐标
                float[] itYs = [
                    Rectangle.Y + radiusY,
                    Rectangle.Y + Rectangle.H - 1 - radiusY,
                    Rectangle.Y + Rectangle.H - 1
                ];

                //左边
                path.AddLine(Rectangle.X, itYs[1], Rectangle.X, itYs[0]);
                //左上角
                path.AddArc(Rectangle.X, Rectangle.Y, 2 * radiusX, 2 * radiusY, 180, 90);

                //上边
                path.AddLine(itXs[0], Rectangle.Y, itXs[1], Rectangle.Y);
                //右上角
                path.AddArc(itXs[1] - radiusX, Rectangle.Y, 2 * radiusX, 2 * radiusY, -90, 90);

                //右边
                path.AddLine(itXs[2], itYs[0], itXs[2], itYs[1]);
                //右下角
                path.AddArc(itXs[1] - radiusX, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 0, 90);

                //下边
                path.AddLine(itXs[1], itYs[2], itXs[0], itYs[2]);
                //左下角
                path.AddArc(Rectangle.X, itYs[1] - radiusY, 2 * radiusX, 2 * radiusY, 90, 90);

                path.CloseFigure();

                return [
                    new Path(path, Border.Create(), Fill?.Create())
                ];
            }
        }

        public override string ToString() => "圆角矩形";
    }
}
